<!DOCTYPE HTML>
<html lang="zh-CN">

<head><meta name="generator" content="Hexo 3.9.0">
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="ydong博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/blog/">
    <link rel="dns-prefetch" href="http://ydongabc.gitee.io/blog">
    <!--SEO-->

<meta name="keywords" content="keep-alive">


<meta name="description" content="&lt;keep-alive&gt; 标签：是Vue的内置组件，能在组件切换过程中将状态保留在内存中，取消组件的销毁函数，防止重复渲染DOM。

当用它包裹 &lt;router-view&gt...">


<meta name="robots" content="all">
<meta name="google" content="all">
<meta name="googlebot" content="all">
<meta name="verify" content="all">
    <!--Title-->

<title>
    
    利用 keep-alive 实现vue页面返回记住滚动条上次浏览位置 |
    
    ydong博客
</title>

<link rel="alternate" href="/atom.xml" title="ydong博客" type="application/atom+xml">


<link rel="icon" href="./favicon.ico">

    

<link rel="stylesheet" href="/blog/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/blog/css/font-awesome.min.css?rev=4.7.0">
<link rel="stylesheet" href="/blog/css/style.css?rev=@@hash">
    
<div class="hide">
    <script type="text/javascript">
    var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
    document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
    </script>
</div>




    

</head></html>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->
<body>
    <header class="main-header"  style="background-image:url(
    /blog/./img/banner.jpg)"
     >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='John Doe'>
            <img src="/blog/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
            <!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
            <img src="/blog/img/branding.png" alt="Snippet 博客主题" class="img-responsive center-block">
            
        </div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                        <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="http://ydongabc.gitee.io/blog">
                        ydong博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog"><i class="fa "></i>
                                首页</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/前端/"><i class="fa "></i>
                                前端</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/后端/"><i class="fa "></i>
                                后端</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/工具/"><i class="fa "></i>
                                工具</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/archives/"><i class="fa "></i>
                                时间轴</a>
                        </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="利用 keep-alive 实现vue页面返回记住滚动条上次浏览位置">
            
            利用 keep-alive 实现vue页面返回记住滚动条上次浏览位置
            
        </h1>
        <div class="post-meta">
    
    <span class="categories-meta fa-wrap">
        <i class="fa fa-folder-open-o"></i>
        <a class="category-link" href="/blog/categories/前端/">前端</a> <a class="category-link" href="/blog/categories/前端/vue/">vue</a>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            <a class="tag-link" href="/blog/tags/keep-alive/">keep-alive</a>
            
        </span>
    </span>
    
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta">
            2019/09/27</span>
    </span>
    
    
</div>
        
        
    </div>
    
    <div class="post-body post-content">
        <p><a href>&lt;keep-alive&gt;</a> 标签：是Vue的内置组件，能在组件切换过程中将状态保留在内存中，取消组件的销毁函数，防止重复渲染DOM<a id="more"></a>。</p>
<ul>
<li>当用它包裹 &lt;router-view&gt; 时，会缓存不活动的组件实例，而不是销毁它们。</li>
<li>和 &lt;transition&gt; 相似，它自身不会渲染一个 DOM 元素。</li>
<li>使用 &lt;keep-alive&gt; 组件后即可使用 activated() 和 deactivated() 这两个生命周期函数。</li>
</ul>
<h2 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a><strong>使用方法</strong></h2><h2 id="1-router-js"><a href="#1-router-js" class="headerlink" title="1.router.js"></a>1.router.js</h2><p>为需要记住滚动条位置的路由组件的 routes:[] 中添加 meta：{keepalive:true}，用于标记该页面组件是否启用 keep-alive 缓存：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123; <span class="attr">path</span>: <span class="string">'/home/list'</span>, <span class="attr">component</span>: List ,<span class="attr">meta</span>:&#123; <span class="attr">keepalive</span>:<span class="literal">true</span> &#125; &#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-app-vue"><a href="#2-app-vue" class="headerlink" title="2.app.vue"></a>2.app.vue</h2><p>用&lt;keep-alive&gt;标签包裹住&lt;router-view&gt;&lt;/router-view&gt;<br>注：如有添加动画效果，&lt;transition&gt; 标签需包裹在最外层</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">transition</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">keep-alive</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">router-view</span> <span class="attr">v-if</span>=<span class="string">"$route.meta.keepalive"</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">keep-alive</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">transition</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">transition</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">router-view</span> <span class="attr">v-if</span>=<span class="string">"!$route.meta.keepalive"</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">transition</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-需要记住浏览位置的页面组件-vue"><a href="#3-需要记住浏览位置的页面组件-vue" class="headerlink" title="3.需要记住浏览位置的页面组件.vue"></a>3.需要记住浏览位置的页面组件.vue</h2><p>在deta里添加一个用于记录位置的数据scrollY：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">data() &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">      scrollY:<span class="number">0</span></span><br><span class="line">  &#125;;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>添加keep-alive相关生命周期函数：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">activated()&#123;	<span class="comment">//进入时读取位置</span></span><br><span class="line">  <span class="built_in">document</span>.body.scrollTop = <span class="keyword">this</span>.scrollY</span><br><span class="line">  <span class="built_in">document</span>.documentElement.scrollTop = <span class="keyword">this</span>.scrollY;</span><br><span class="line">&#125;,</span><br><span class="line">deactivated()&#123;	<span class="comment">//离开时记录位置</span></span><br><span class="line">  <span class="keyword">this</span>.scrollY = <span class="built_in">document</span>.body.scrollTop;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="4-如果有需要使进入的新页面的滚动条位置-初始化置顶，即指定单个页面的滚动条位置"><a href="#4-如果有需要使进入的新页面的滚动条位置-初始化置顶，即指定单个页面的滚动条位置" class="headerlink" title="4.如果有需要使进入的新页面的滚动条位置 初始化置顶，即指定单个页面的滚动条位置"></a>4.如果有需要使进入的新页面的滚动条位置 初始化置顶，即指定单个页面的滚动条位置</h2><p>可在新页面组件中的mounted生命周期函数中添加：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123; <span class="comment">//初始化滚动条置顶</span></span><br><span class="line">  <span class="built_in">document</span>.body.scrollTop = <span class="number">0</span>;</span><br><span class="line">  <span class="built_in">document</span>.documentElement.scrollTop = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>
    
    <div class="post-footer">
        <div>
            
            转载声明：
            商业转载请联系作者获得授权,非商业转载请注明出处 © <a href="" target="_blank">Snippet</a>
            
            
        </div>
        <div>
            
        </div>
    </div>
</article>
<div class="article-nav prev-next-wrap clearfix">
    
    <a href="/blog/2019/10/02/弹性布局flex/" class="pre-post btn btn-default" title='弹性布局flex'>
        <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
        <span class="hidden-xs">
            弹性布局flex</span>
    </a>
    
    
    <a href="/blog/2019/09/26/vue-cli脚手架的基本命令和使用/" class="next-post btn btn-default" title='vue-cli脚手架的基本命令和使用'>
        <span class="hidden-lg">下一篇</span>
        <span class="hidden-xs">
            vue-cli脚手架的基本命令和使用</span><i class="fa fa-angle-right fa-fw"></i>
    </a>
    
</div>

                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">
            文章目录
        </h3>
        
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#使用方法"><span class="toc-text">使用方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-router-js"><span class="toc-text">1.router.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-app-vue"><span class="toc-text">2.app.vue</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-需要记住浏览位置的页面组件-vue"><span class="toc-text">3.需要记住浏览位置的页面组件.vue</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-如果有需要使进入的新页面的滚动条位置-初始化置顶，即指定单个页面的滚动条位置"><span class="toc-text">4.如果有需要使进入的新页面的滚动条位置 初始化置顶，即指定单个页面的滚动条位置</span></a></li></ol>
        
    </div>
</aside>
                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>
<a id="back-to-top" class="icon-btn hide">
    <i class="fa fa-chevron-up"></i>
</a>
    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
</div>
            </div>
            <div class="col-sm-12">
                <span>Copyright &copy;
                    2017
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>



<script src="/blog/js/app.js?rev=@@hash"></script>
</body>
</html>